<?php
/*
Plugin Name: Twitter Tracker
Plugin URI: http://simonwheatley.co.uk/wordpress/twitter-tracker
Description: Tracks the search results on <a href="http://search.twitter.com/" target="_blank">Twitter search</a> in a sidebar widget.
Author: Simon Wheatley
Version: 2.01
Author URI: http://simonwheatley.co.uk/wordpress/
*/

// http://search.twitter.com/search.atom?q=wordcampuk

/*  Copyright 2008 Simon Wheatley

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

require_once( dirname (__FILE__) . '/plugin.php' );
require_once( dirname (__FILE__) . '/class-TwitterTracker_Widget.php' );

/**
 *
 * @package default
 * @author Simon Wheatley
 **/
class TwitterTracker extends TwitterTracker_Plugin
{

	public $widget;

	public function __construct()
	{
		if ( is_admin() ) {
			$this->register_activation (__FILE__);
		}
		// Init
		$this->register_plugin ( 'twitter-tracker', __FILE__ );
		$this->add_action( 'init' );

		// register widget
		add_action('widgets_init', create_function('', 'return register_widget( "TwitterTracker_Widget" );'));
	}
	
	// HOOKS
	// =====
	
	public function activate()
	{
		// Empty
	}
	
	public function init()
	{
		// Slightly cheeky, but change the cache age of Magpie from 60 minutes to 15 minutes
		// That's still plenty of caching IMHO :)
		if ( !defined('MAGPIE_CACHE_AGE') ) {
			define('MAGPIE_CACHE_AGE', 60 * 15); // Fifteen of your Earth minutes
		}
	}
	
	public function show( $instance )
	{
		extract( $instance );
		// Let the user know if there's no search query
		if ( empty( $twitter_search ) ) {
			$this->render( 'widget-error', array() );
			return;
		}
		require_once( dirname( __FILE__ ) . '/model/twitter-search.php' );
		require_once( dirname( __FILE__ ) . '/model/tweet.php' );
		$search = new TwitterSearch ( $twitter_search, $max_tweets, $hide_replies );
		$vars = array( 
			'tweets' => $search->tweets(), 
			'preamble' => $preamble,
			'html_after' => $html_after
			 );
		$vars[ 'datef' ] = _c( 'M j, Y @ G:i|Publish box date format');
		$this->render( 'widget-contents', $vars );
	}

	public function & get()
	{
	    static $instance;

	    if ( ! isset ( $instance ) ) {
			$c = __CLASS__;
			$instance = new $c;
	    }

	    return $instance;
	}

}

function twitter_tracker( $instance )
{
	$tracker = TwitterTracker::get();
	$tracker->show( $instance );
}


/**
 * Instantiate the plugin
 *
 * @global
 **/

$TwitterTracker = new TwitterTracker();

?>